package canglang.monitor.dept.dao;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

import canglang.monitor.core.base.dao.BaseDAO;
import canglang.monitor.core.base.util.StringUtil;
import canglang.monitor.core.base.pagination.PaginatedListHelper;
import canglang.monitor.dept.model.DeptModel;
import canglang.monitor.dept.to.SearchTO;

@SuppressWarnings("unchecked")
public class DeptDAO extends BaseDAO {

	public  Integer addDept(DeptModel deptModel){
        String sql="addDept";
        Map map=objectToMap(deptModel);
        return (Integer)getSqlMapClientTemplate().insert(sql,map);
    }
	
	public DeptModel getDept(String deptName){
        String sql="getDept";
        Map map=new HashMap();
        map.put("deptName", deptName);
        return (DeptModel)getSqlMapClientTemplate().queryForObject(sql,map);
    }
	
	public DeptModel getDept(int deptId){
        String sql="getDept";
        Map map=new HashMap();
        map.put("deptId", deptId);
        return (DeptModel)getSqlMapClientTemplate().queryForObject(sql,map);
    }
	
    public DeptModel saveDept(DeptModel deptModel){
        String sql="saveDept";
        Map map=objectToMap(deptModel);
        getSqlMapClientTemplate().update(sql,map);
        return this.getDept(deptModel.getDeptId());
    }	
	
    public void deleteDept(int deptId){
        String sql="deleteDept";
        Map map=new HashMap();
        map.put("deptId", deptId);
        getSqlMapClientTemplate().delete(sql,map);
    }

    public Integer searchCount(SearchTO searchTO) {
        Map map = new HashMap();
        String deptName=StringUtil.convertEmpty2Null(searchTO.getDeptName());
        if(deptName!=null)deptName="%"+deptName+"%";
        map.put("deptName", deptName);
        return (Integer) getSqlMapClientTemplate().queryForObject("searchDeptCount", map);
    }

    public List searchList(PaginatedListHelper helper, SearchTO searchTO) {
        Map map = new HashMap();
        String deptName=StringUtil.convertEmpty2Null(searchTO.getDeptName());
        if(deptName!=null)deptName="%"+deptName+"%";
        map.put("deptName", deptName);
        map = constructPaginationMap(helper, map);
        return getSqlMapClientTemplate().queryForList("searchDeptList", map);
    }
}
